
package com.cn.ZhiKeDianShang.util;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.cn.ZhiKeDianShang.R;


/**
 * 创    建:  lt  2018/1/8--11:51
 * 作    用:  对话框工具类
 * 注意事项:
 */


public class CommontDialog {


    /**
     * 仿抖音个人中心   弹出 拉黑 发私信  举报功能
     *
     * @return
     */
    public static AlertDialog UtilsDialog(Context context, View.OnClickListener sendMessageListener, View.OnClickListener reportListener) {
        View view = LayoutInflater.from(context).inflate(R
                .layout.videopersonutils_dialog_layout, null);
        TextView tv_sendmessage = (TextView) view.findViewById(R.id.tv_sendmessage);
        TextView tv_report = (TextView) view.findViewById(R.id.tv_report);
        TextView tv_cancle = (TextView) view.findViewById(R.id.tv_cancle);

        tv_sendmessage.setOnClickListener(sendMessageListener);
        tv_report.setOnClickListener(reportListener);

        //点击事件
        AlertDialog dialog = new AlertDialog.Builder(context).create();
        dialog.show();
        tv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });


        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.Animation);
        window.setBackgroundDrawable(new ColorDrawable(0));
        window.setContentView(view);
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        dialog.setCanceledOnTouchOutside(true);
        //使弹出输入法
        InputMethodManager im2 = (InputMethodManager) context
                .getSystemService(Context.INPUT_METHOD_SERVICE);
//        im2.showSoftInput(mInput, InputMethodManager.SHOW_FORCED);
        dialog.getWindow()
                .clearFlags(
                        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                                | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        dialog.getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        return dialog;
    }


    /**
     * 处理关联接口
     *
     * @return
     */
    public static AlertDialog DealRealte(Context context, View.OnClickListener sendMessageListener, View.OnClickListener reportListener) {
        View view = LayoutInflater.from(context).inflate(R
                .layout.dealrelate_dialog_layout, null);
        TextView tv_commit = (TextView) view.findViewById(R.id.tv_commit);
        TextView tv_cancle = (TextView) view.findViewById(R.id.tv_cancle);
        ImageView im_close = (ImageView) view.findViewById(R.id.im_close);

        tv_commit.setOnClickListener(sendMessageListener);
        tv_cancle.setOnClickListener(reportListener);

        //点击事件
        AlertDialog dialog = new AlertDialog.Builder(context).create();
        dialog.show();
        im_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });


        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER);
        window.setWindowAnimations(R.style.Animation);
        window.setBackgroundDrawable(new ColorDrawable(0));
        window.setContentView(view);
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        dialog.setCanceledOnTouchOutside(true);
        //使弹出输入法
        InputMethodManager im2 = (InputMethodManager) context
                .getSystemService(Context.INPUT_METHOD_SERVICE);
//        im2.showSoftInput(mInput, InputMethodManager.SHOW_FORCED);
        dialog.getWindow()
                .clearFlags(
                        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                                | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        dialog.getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        return dialog;
    }


    /**
     * 选择团队
     *
     * @return
     */
    public static AlertDialog selectTeam(Context context, View.OnClickListener sendMessageListener, View.OnClickListener reportListener) {
        View view = LayoutInflater.from(context).inflate(R
                .layout.dealrelate_dialog_layout, null);
        TextView tv_commit = (TextView) view.findViewById(R.id.tv_commit);
        TextView tv_cancle = (TextView) view.findViewById(R.id.tv_cancle);
        ImageView im_close = (ImageView) view.findViewById(R.id.im_close);

        tv_commit.setOnClickListener(sendMessageListener);
        tv_cancle.setOnClickListener(reportListener);

        //点击事件
        AlertDialog dialog = new AlertDialog.Builder(context).create();
        dialog.show();
        im_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });


        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER);
        window.setWindowAnimations(R.style.Animation);
        window.setBackgroundDrawable(new ColorDrawable(0));
        window.setContentView(view);
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        dialog.setCanceledOnTouchOutside(true);
        //使弹出输入法
        InputMethodManager im2 = (InputMethodManager) context
                .getSystemService(Context.INPUT_METHOD_SERVICE);
//        im2.showSoftInput(mInput, InputMethodManager.SHOW_FORCED);
        dialog.getWindow()
                .clearFlags(
                        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                                | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        dialog.getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        return dialog;
    }


    /**
     * 取消任务
     *
     * @return
     */
    public static AlertDialog cacleTask(Context context,String text, View.OnClickListener sendMessageListener) {
        View view = LayoutInflater.from(context).inflate(R
                .layout.cancletask_dialog_layout, null);
        TextView tv_commit = (TextView) view.findViewById(R.id.tv_commit);
        TextView tv_cancle = (TextView) view.findViewById(R.id.tv_cancle);
        EditText et_number = (EditText) view.findViewById(R.id.et_number);
        et_number.setHint(text);
        ImageView im_close = (ImageView) view.findViewById(R.id.im_close);
        tv_commit.setOnClickListener(sendMessageListener);
        //点击事件
        AlertDialog dialog = new AlertDialog.Builder(context).create();
        dialog.show();
        tv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

        im_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER);
        window.setWindowAnimations(R.style.Animation);
        window.setBackgroundDrawable(new ColorDrawable(0));
        window.setContentView(view);
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        dialog.setCanceledOnTouchOutside(true);
        //使弹出输入法
        InputMethodManager im2 = (InputMethodManager) context
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        im2.showSoftInput(et_number, InputMethodManager.SHOW_FORCED);
        dialog.getWindow()
                .clearFlags(
                        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                                | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        dialog.getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        return dialog;
    }


    /**
     * 添加关联
     *
     * @return
     */
    static RealteClick mRealteClick;

    public static AlertDialog RealteUtilsDialog(Context context, RealteClick realteClick) {
        mRealteClick = realteClick;
        View view = LayoutInflater.from(context).inflate(R
                .layout.addrelate_dialog_layout, null);
        TextView tv_commit = (TextView) view.findViewById(R.id.tv_commit);
        TextView tv_cancle = (TextView) view.findViewById(R.id.tv_cancle);
        EditText et_number = (EditText) view.findViewById(R.id.et_number);
        ImageView im_close = (ImageView) view.findViewById(R.id.im_close);

        tv_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (et_number.getText().toString().length() == 0) {
                    ToastUtil.showShort("请输入团队关联码");
                    return;
                }
                mRealteClick.commit(et_number.getText().toString());
            }
        });
        tv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRealteClick.calcle();
            }
        });

        //点击事件
        AlertDialog dialog = new AlertDialog.Builder(context).create();
        dialog.show();
        tv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

        im_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER);
        window.setWindowAnimations(R.style.Animation);
        window.setBackgroundDrawable(new ColorDrawable(0));
        window.setContentView(view);
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        dialog.setCanceledOnTouchOutside(true);
        //使弹出输入法
        InputMethodManager im2 = (InputMethodManager) context
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        im2.showSoftInput(et_number, InputMethodManager.SHOW_FORCED);
        dialog.getWindow()
                .clearFlags(
                        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                                | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        dialog.getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        return dialog;
    }


    public interface RealteClick {
        void commit(String text);

        void calcle();
    }

    /**
     * 将dip或dp值转换为px值，保证尺寸大小不变
     *
     * @param dipValue dp尺寸
     * @return
     */

    public static int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }


}

